//import 'dart:html';

import 'package:flutter/material.dart';
import 'package:gzx_dropdown_menu/gzx_dropdown_menu.dart';
import 'package:magic_car/baoxiu/guarantee.dart';
import 'package:magic_car/bussiness/bussinessinspect.dart';
import 'package:magic_car/common/nodata.dart';
import 'package:magic_car/jiance/b_editcar.dart';
import 'package:magic_car/util/HttpHelper.dart';
import 'package:magic_car/util/colors.dart';
import 'package:magic_car/zhanshi/cheyuandetail.dart';
import 'package:shared_preferences/shared_preferences.dart';
import 'package:cached_network_image/cached_network_image.dart';
import 'package:fluwx/fluwx.dart';
import 'package:toast/toast.dart';

class SortCondition {
  String name;
  bool isSelected;
  String tag;

  SortCondition({this.name, this.isSelected, this.tag});
}

class MycarPage extends StatefulWidget {
  @override
  _GZXDropDownMenuTestPageState createState() =>
      _GZXDropDownMenuTestPageState();
}

class _GZXDropDownMenuTestPageState extends State<MycarPage> {
  int page = 1;
  List car = new List();
  // List<String> _dropDownHeaderItemStrings = ['类型', '品牌', '价格', '筛选'];
  List<String> _dropDownHeaderItemStrings = ['品牌', '车型', '价格'];
  List<SortCondition> _brandSortConditions = [];
  List<SortCondition> _distanceSortConditions = [];
  SortCondition _selectBrandSortCondition;
  SortCondition _selectDistanceSortCondition;
  GZXDropdownMenuController _dropdownMenuController =
      GZXDropdownMenuController();
  ScrollController _scrollController = new ScrollController();
  var _scaffoldKey = new GlobalKey<ScaffoldState>();
  GlobalKey _stackKey = GlobalKey();

  List BrandList = new List();
  List secondList = new List();
  String brand = "";
  String cartype = "";
  String price = "";
  WeChatScene scene = WeChatScene.SESSION;
  List view_arr = new List();
  bool expand = true;
  bool checkAll = false;
  String idStrs = "";
  String shareUrl = "";
  bool hasmore = false;
  @override
  void initState() {
    view_arr = [
      {
        'title': '微信好友',
        'icon': 'resource/images/user/wechat.jpeg',
        'tag': WeChatScene.SESSION
      },
      {
        'title': '朋友圈',
        'icon': 'resource/images/user/wechat1.jpeg',
        'tag': WeChatScene.TIMELINE
      }
    ];
    super.initState();
    _scrollController.addListener(() {
      if (_scrollController.position.pixels ==
          _scrollController.position.maxScrollExtent) {
        loadMore();
      }
    });
    _brandSortConditions.add(SortCondition(name: '全部', isSelected: true));
    _brandSortConditions.add(SortCondition(name: '轿车', isSelected: false));
    _brandSortConditions.add(SortCondition(name: '两厢轿车', isSelected: false));
    _brandSortConditions.add(SortCondition(name: '面包车', isSelected: false));
    _brandSortConditions.add(SortCondition(name: '跑车', isSelected: false));
    _brandSortConditions.add(SortCondition(name: '皮卡', isSelected: false));
    _brandSortConditions.add(SortCondition(name: '三厢轿车', isSelected: false));
    _brandSortConditions.add(SortCondition(name: '商务车/MPV', isSelected: false));
    _brandSortConditions.add(SortCondition(name: 'SUV/越野车', isSelected: false));

    _selectBrandSortCondition = _brandSortConditions[0];

    // _distanceSortConditions.add(SortCondition(name: '距离近', isSelected: true));
    _distanceSortConditions
        .add(SortCondition(name: '价格由低至高', isSelected: false, tag: "0"));
    _distanceSortConditions
        .add(SortCondition(name: '价格由高至低', isSelected: false, tag: "1"));
    _distanceSortConditions
        .add(SortCondition(name: '5万以下', isSelected: false, tag: "0-5"));
    _distanceSortConditions
        .add(SortCondition(name: '5万-10万', isSelected: false, tag: "5-10"));
    _distanceSortConditions
        .add(SortCondition(name: '10万-20万', isSelected: false, tag: "10-20"));
    _distanceSortConditions
        .add(SortCondition(name: '20万-30万', isSelected: false, tag: "20-30"));
    _distanceSortConditions
        .add(SortCondition(name: '30万-50万', isSelected: false, tag: "30-50"));
    _distanceSortConditions
        .add(SortCondition(name: '50万以上', isSelected: false, tag: "50-10000"));

    _selectDistanceSortCondition = _distanceSortConditions[0];
    loaddata();
    loadBrand();
  }

  loadBrand() {
    Map<String, String> map = new Map();

    HttpHelper.post(HttpHelper.API_Brand, (data) async {
      //loadpage.close();
      if (data != null) {
        print(data);
        //Toast.show(data["msg"], context);

        if (data["code"].toString() == "1") {
          List temp = data["info"];

          setState(() {
            BrandList = temp;
            //  hotList = hotList;
            //  loaddata=true;
          });
        }
      }
    }, params: map);
  }

//加载更多数据
  loadMore() {
    if (hasmore) {
      loaddata();
    }
  }

  // 下拉刷新方法,为list重新赋值
  Future _onRefresh() async {
    await Future.delayed(Duration(seconds: 1), () {
      print('refresh');
      setState(() {
        page = 1;
      });
      loaddata();
    });
  }

  //
  sharehandel() {
    if (car.length == 0) {
      Toast.show('没有任何车源', context);
    } else {
      var idStr = "";
      var shareurl = "";
      for (var i = 0; i < car.length; i++) {
        shareurl = car[i]["sharelist"];
        if (car[i]["select"] == "1") {
          idStr += car[i]['id'].toString() + ',';
        }
      }
      if (idStr == "") {
        Toast.show('请选择要分享的车源', context);
      } else {
        setState(() {
          idStrs = idStr;
          shareUrl = shareurl;
        });
        showModalBottomSheet(
            context: context,
            builder: (BuildContext context) {
              return _shareWidget();
            });
      }
    }
  }

  //分享
  share() {
    String describle = "";

    var zhengchezhaopian = "";
    // for (var value in pics['post']) {
    //   if (value['name'] == '整车照片') {
    //     zhengchezhaopian = value['url'];
    //   }
    // }

    print(shareUrl + '?id=' + idStrs);

    var model = WeChatShareWebPageModel(
      HttpHelper.host +
          '/app/index.php?i=16&j=16&c=entry&m=chemogu&do=mobile&r=car.carlist' +
          '&id=' +
          idStrs,
      title: '我的车源信息',
      description: '我的车源',
      // description: '233',
      thumbnail: zhengchezhaopian != ""
          ? WeChatImage.network(zhengchezhaopian)
          : WeChatImage.asset('resource/images/user/1024.png'),
      scene: scene,
    );
    shareToWeChat(model);
  }

  Widget _shareWidget() {
    return new Container(
      height: 160.0,
      child: new Column(
        children: <Widget>[
          new Padding(
            padding: EdgeInsets.fromLTRB(0.0, 10.0, 0.0, 0.0),
            child: new Container(
              height: 100.0,
              child: new GridView.builder(
                gridDelegate: new SliverGridDelegateWithFixedCrossAxisCount(
                    crossAxisCount: 2,
                    mainAxisSpacing: 1.0,
                    childAspectRatio: 1.0),
                itemBuilder: (BuildContext context, int index) {
                  var item = view_arr[index];
                  return new GestureDetector(
                      onTap: () {
                        //print(item['tag']);
                        Navigator.of(context).pop();
                        //选择检测师
                        setState(() {
                          scene = item['tag'];
                          share();
                        });
                      },
                      child: new Column(
                        crossAxisAlignment: CrossAxisAlignment.center,
                        mainAxisAlignment: MainAxisAlignment.center,
                        children: <Widget>[
                          new Padding(
                              padding: EdgeInsets.fromLTRB(0.0, 6.0, 0.0, 6.0),
                              child: new Container(
                                  decoration: new BoxDecoration(
                                    // 底色
                                    //        borderRadius: new BorderRadius.circular((20.0)), // 圆角度
                                    borderRadius: new BorderRadius.circular(
                                        25), // 也可控件一边圆角大小
                                  ),
                                  height: 50,
                                  width: 50,
                                  child: ClipRRect(
                                      borderRadius: BorderRadius.circular(25),
                                      child: Image.asset(item['icon'])
                                      // child: FadeInImage.assetNetwork(
                                      //   placeholder: item['icon'],
                                      //   image: '',
                                      //   fit: BoxFit.cover,
                                      // ),
                                      ))),
                          Expanded(child: new Text(item['title']))
                        ],
                      ));
                },
                itemCount: view_arr.length,
              ),
            ),
          ),
          new Container(
            height: 0.5,
            color: Colors.blueGrey,
          ),
          new Center(
            child: new Padding(
              padding: EdgeInsets.fromLTRB(0.0, 8.0, 0.0, 8.0),
              child: GestureDetector(
                  behavior: HitTestBehavior.opaque,
                  onTap: () {
                    Navigator.of(context).pop();
                    setState(() {});
                  },
                  child: new Text(
                    '取  消',
                    style:
                        new TextStyle(fontSize: 18.0, color: Colors.blueGrey),
                  )),
            ),
          ),
        ],
      ),
    );
  }

  loaddata() async {
    SharedPreferences pref = await SharedPreferences.getInstance();
    Map<String, String> map = new Map();
    map["page"] = this.page.toString();
    map["brand"] = this.brand;
    map["cartype"] = this.cartype;
    map["price"] = this.price;
    map['bid'] = pref.getString('uid');
    print(map);

    HttpHelper.post(HttpHelper.ApI_CarList, (data) async {
      //loadpage.close();
      if (data != null) {
        print(data);
        //Toast.show(data["msg"], context);
        if (data["code"].toString() == "1") {
          var info = data["info"];

          if (this.page > 1) {
            setState(() {
              car.addAll(info);
            });

            // List temp = info;
            // List temparr = this.car;
            // if(temp.length>0){
            // for (var item in temp) {
            //   temparr.add(item);
            // }
            // setState(() {
            //   car = temparr;
            // });
            // }
          } else {
            setState(() {
              // status = info["status"].toString();
              car = info;
            });
          }

          if (info.length == 10) {
            setState(() {
              hasmore = true;
              page = page + 1;
            });
          } else {
            setState(() {
              hasmore = false;
            });
          }

          print(car.length.toString());
        }
        // if (data["code"].toString() == "1") {
        //   var info = data["info"];

        //   if (this.page > 1) {
        //     List temp = info;
        //     for (var item in temp) {
        //       this.car.add(item);
        //     }
        //     setState(() {
        //       car = car;
        //     });
        //   } else {
        //     setState(() {
        //       // status = info["status"].toString();
        //       car = info;
        //     });
        //   }
        // }
      }
    }, params: map);
  }

  //自检订单
  precheckgroup(id) async {
    SharedPreferences pref = await SharedPreferences.getInstance();
    Map<String, String> map = new Map();
    map["uid"] = pref.getString('uid');
    map['type'] = "1";

    HttpHelper.post(HttpHelper.API_PrecheckGroup, (data) async {
      //loadpage.close();
      if (data != null) {
        print(data);
        //Toast.show(data["msg"], context);

        if (data["code"].toString() == "1") {
          // showalert();
          Navigator.of(context).push(new MaterialPageRoute(builder: (_) {
            return new BussinessPapularinspects(id, 2, {});
          }));
        } else {
          Toast.show(data["msg"], context);
        }
      }
    }, params: map);
  }

  //认证检测下单
  submitauthorder(id) async {
    Map<String, String> map = new Map();
    SharedPreferences pref = await SharedPreferences.getInstance();
    map["uid"] = pref.getString('uid');
    map["oid"] = id;

    print(map);

    HttpHelper.post(HttpHelper.API_Create_auth_by_car, (data) async {
      if (data != null) {
        Toast.show(data["msg"], context, duration: 2);
        if (data["code"] == 1) {
        //  Navigator.pop(context);
        }
      }
    }, params: map);
  }

  //查看车辆是否能下保修订单
  checkcanbaoxiu(id) async {
    Map<String, String> map = new Map();
    SharedPreferences pref = await SharedPreferences.getInstance();
    map["uid"] = pref.getString('uid');
    map["car_id"] = id;

    print(map);

    HttpHelper.post(HttpHelper.API_PrecheckBaoxiu, (data) async {
      if (data != null) {
        
        if (data["code"] == 1) {
          Navigator.of(context).push(new MaterialPageRoute(builder: (_) {
            return new GuaranteePage(id, 1);
          }));
          //  Navigator.pop(context);
        }else{
          Toast.show(data["msg"], context, duration: 2);
        }
      }
    }, params: map);
  }

  @override
  Widget build(BuildContext context) {
    print('_GZXDropDownMenuTestPageState.build');

    return Scaffold(
      key: _scaffoldKey,
      appBar: PreferredSize(
          child: AppBar(
            title: Text('我的车源11'),
            // actions: <Widget>[
            //   IconButton(
            //     icon: Icon(Icons.share),
            //     onPressed: () {
            //       showModalBottomSheet(
            //           context: context,
            //           builder: (BuildContext context) {
            //             return _shareWidget();
            //           });
            //     },
            //   ),
            // ],
            backgroundColor: Colours.app_main,
            elevation: 0,
          ),
          preferredSize: Size.fromHeight(0)),
      backgroundColor: Colors.white,
      // floatingActionButtonLocation: FloatingActionButtonLocation.endFloat,
      // floatingActionButton: FloatingActionButton(
      //   backgroundColor: Colors.blue,
      //   onPressed: () {
      //     if(car.length==0){
      //       Toast.show('没有任何车源', context);
      //     }else{
      //       var idStr = "";
      //       var shareurl = "";
      //       for(var i =0;i<car.length;i++){
      //         shareurl = car[i]["sharelist"];
      //        if(car[i]["select"]=="1"){
      //          idStr+=car[i]['id'].toString()+',';
      //        }
      //       }
      //       if(idStr==""){
      //       Toast.show('请选择要分享的车源', context);
      //       }else{
      //         setState(() {
      //           idStrs = idStr;
      //           shareUrl = shareurl;
      //         });
      //       showModalBottomSheet(
      //                 context: context,
      //                 builder: (BuildContext context) {
      //                   return _shareWidget();
      //                 });
      //       }
      //     }

      //     // backhome();
      //     setState(() {
      //      // expand=true;

      //     });
      //   },
      //   child: Icon(
      //     Icons.share,
      //     color: Colors.white,
      //   ),
      // ),

      endDrawer: Container(
        margin: EdgeInsets.only(
            left: MediaQuery.of(context).size.width / 4, top: 0),
        color: Colors.white,
//        child: Container(color: Colors.red,child: Padding(
//          padding: const EdgeInsets.all(8.0),
//          child: TextField(),
//        ),),
        child: ListView(
          children: <Widget>[TextField()],
        ),
      ),
      body: Stack(
        key: _stackKey,
        children: <Widget>[
          Column(
            children: <Widget>[
              Container(
                width: MediaQuery.of(context).size.width,
                height: 60,
                // color: Color.fromRGBO(7, 169, 251, 1),
                color: Colours.app_main,
                alignment: Alignment.center,
                child: Row(
                    crossAxisAlignment: CrossAxisAlignment.start,
                    mainAxisAlignment: MainAxisAlignment.start,
                    children: <Widget>[
                      Align(
                          alignment: Alignment.center,
                          child: IconButton(
                              onPressed: () {
                                Navigator.pop(context);
                              },
                              icon: Icon(
                                Icons.arrow_back,
                                color: Colors.white,
                              ))),
                      Align(
                        alignment: Alignment.center,
                        child: Text('我的车源',
                            style: TextStyle(
                              fontSize: 20,
                              color: Colors.white,
                            )),
                      ),

                      //  Align(
                      //   alignment: Alignment.topLeft,
                      //   child: Text('我的车源',
                      //       style: TextStyle(
                      //         fontSize: 20,
                      //         color: Colors.white,
                      //       )),
                      // ),
                      // Positioned(
                      //   right: 20,
                      //     child: Text('我的车源11',
                      //       style: TextStyle(
                      //         fontSize: 20,
                      //         color: Colors.white,
                      //       )),
                      // )
                      // Align(
                      //   alignment: Alignment.centerRight,
                      //   child: Text('我的车源',
                      //       style: TextStyle(
                      //         fontSize: 20,
                      //         color: Colors.white,
                      //       )),
                      // )
                    ]),
              ),
//              SizedBox(height: 20,),
              // 下拉菜单头部
              GZXDropDownHeader(
                // 下拉的头部项，目前每一项，只能自定义显示的文字、图标、图标大小修改
                items: [
                  GZXDropDownHeaderItem(_dropDownHeaderItemStrings[0]),
                  GZXDropDownHeaderItem(_dropDownHeaderItemStrings[1]),
                  GZXDropDownHeaderItem(_dropDownHeaderItemStrings[2]),
                  // GZXDropDownHeaderItem(_dropDownHeaderItemStrings[3],
                  //     iconData: Icons.filter_frames, iconSize: 18),
                ],
                // GZXDropDownHeader对应第一父级Stack的key
                stackKey: _stackKey,
                // controller用于控制menu的显示或隐���
                controller: _dropdownMenuController,
                // 当点击头部项的事件，在这里可以进行页面跳转或openEndDrawer
                onItemTap: (index) {
                  //控制展现方式
                  // if (index == 3) {
                  //   _scaffoldKey.currentState.openEndDrawer();
                  // }
                },
//                // 头部的高度
//                height: 40,
//                // 头部背景颜色
//                color: Colors.red,
//                // 头部边框宽度
//                borderWidth: 1,
//                // 头部边框颜色
//                borderColor: Color(0xFFeeede6),
//                // 分割线高度
//                dividerHeight: 20,
//                // 分割线颜色
//                dividerColor: Color(0xFFeeede6),
//                // 文字样式
//                style: TextStyle(color: Color(0xFF666666), fontSize: 13),
//                // 下拉时文字样式
//                dropDownStyle: TextStyle(
//                  fontSize: 13,
//                  color: Theme.of(context).primaryColor,
//                ),
//                // ���标大小
//                iconSize: 20,
//                // 图标颜色
//                iconColor: Color(0xFFafada7),
//                // 下拉时图标颜色
//                iconDropDownColor: Theme.of(context).primaryColor,
              ),
              Expanded(
                  child: Stack(children: <Widget>[
                new RefreshIndicator(
                  onRefresh: _onRefresh,
                  child: car.length == 0 && page == 1
                      ? Nodata()
                      : ListView.separated(
                          controller: _scrollController,
                          itemCount: car.length,
                          padding: new EdgeInsets.fromLTRB(0, 0, 0, 40),
                          physics: AlwaysScrollableScrollPhysics(),
                          separatorBuilder: (BuildContext context, int index) =>
                              Divider(height: 1.0),
                          itemBuilder: (BuildContext context, int index) {
                            return Padding(
                              padding: new EdgeInsets.fromLTRB(10, 10, 10, 0),
                              child: new Container(
                                  child: GestureDetector(
                                      behavior: HitTestBehavior.opaque,
                                      onTap: () {
                                        // showModalBottomSheet(
                                        //     context: context,
                                        //     builder: (BuildContext context) {
                                        //       return _shareWidget();
                                        //     });
                                        Navigator.push(
                                          context,
                                          new MaterialPageRoute(
                                              builder: (context) =>
                                                  new CardetailPage(
                                                      car[index], 1)),
                                        );
                                      },
                                      child: new Row(
                                        children: <Widget>[
                                          car[index]['car_status'].toString() !=
                                                  "2"
                                              ? new Container(
                                                  height: 0, width: 0)
                                              : checkAll
                                                  ? SizedBox(
                                                      width: 22,
                                                      child: new Checkbox(
                                                          value: car[index][
                                                                      "select"] ==
                                                                  "1"
                                                              ? true
                                                              : false,
                                                          onChanged: (check) {
                                                            if (check ==
                                                                false) {
                                                              car[index][
                                                                      "select"] =
                                                                  "0";
                                                            } else {
                                                              car[index][
                                                                      "select"] =
                                                                  "1";
                                                            }
                                                            setState(() {
                                                              car = car;
                                                            });
                                                          }))
                                                  : Container(height: 0),

                                          // expand == false
                                          //     ? new Container(
                                          //         height: 0,
                                          //       )
                                          //     : Positioned(
                                          //         child: new Checkbox(
                                          //             value: car[index]
                                          //                         ["select"] ==
                                          //                     "1"
                                          //                 ? true
                                          //                 : false,
                                          //             onChanged: (check) {
                                          //               if (check == false) {
                                          //                 car[index]["select"] =
                                          //                     "0";
                                          //               } else {
                                          //                 car[index]["select"] =
                                          //                     "1";
                                          //               }
                                          //               setState(() {
                                          //                 car = car;
                                          //               });
                                          //             }),
                                          //         left: 10,
                                          //       ),
                                          Expanded(
                                              flex: 1,
                                              child: (new Container(
                                                width: 120,
                                                height: 100,
                                                child:
                                                    car[index]["thumb"] != null
                                                        ? Image.network(
                                                            car[index]["thumb"],
                                                            fit: BoxFit.cover,
                                                          )
                                                        : Container(
                                                            height: 0,
                                                          ),
                                              ))),
                                          Expanded(
                                            flex: 2,
                                            child: new Column(
                                              children: <Widget>[
                                                new Container(
                                                  alignment: Alignment.topLeft,
                                                  child: new Padding(
                                                      padding: new EdgeInsets
                                                              .fromLTRB(
                                                          10, 0, 0, 0),
                                                      child: Text(
                                                        car[index]["title"],
                                                        style: TextStyle(
                                                            color:
                                                                Color.fromRGBO(
                                                                    16,
                                                                    29,
                                                                    55,
                                                                    1),
                                                            fontSize: 15),
                                                      )),
                                                ),
                                                int.parse(car[index]
                                                            ["auth_status"]) >
                                                        0
                                                    ? new Padding(
                                                        padding: new EdgeInsets
                                                                .fromLTRB(
                                                            10, 8, 0, 0),
                                                        child: new Row(
                                                          crossAxisAlignment:
                                                              CrossAxisAlignment
                                                                  .start,
                                                          mainAxisAlignment:
                                                              MainAxisAlignment
                                                                  .start,
                                                          children: <Widget>[
                                                            Image.asset(
                                                                'resource/images/baoxiu/inspected.png',
                                                                width: 15,
                                                                height: 17),
                                                            Padding(
                                                                padding:
                                                                    new EdgeInsets
                                                                            .fromLTRB(
                                                                        5,
                                                                        0,
                                                                        0,
                                                                        0),
                                                                child: Text(
                                                                    car[index]["auth_status"] ==
                                                                            "1"
                                                                        ? '已认证'
                                                                        : "认证检测中",
                                                                    style:
                                                                        TextStyle(
                                                                      fontWeight:
                                                                          FontWeight
                                                                              .w700,
                                                                      fontSize:
                                                                          11,
                                                                      color: car[index]["auth_status"] ==
                                                                              "1"
                                                                          ? Color.fromRGBO(
                                                                              18,
                                                                              150,
                                                                              219,
                                                                              1)
                                                                          : Colours
                                                                              .bj_orange_o1,
                                                                    ))),
                                                            car[index]["is_guratee"]
                                                                        .toString() ==
                                                                    "1"
                                                                ? Padding(
                                                                    padding:
                                                                        new EdgeInsets.fromLTRB(
                                                                            15,
                                                                            0,
                                                                            0,
                                                                            0),
                                                                    child: Image.asset(
                                                                        'resource/images/baoxiu/guarateed.png',
                                                                        width:
                                                                            15,
                                                                        height:
                                                                            17))
                                                                : Container(
                                                                    height: 0),
                                                            car[index]["is_guratee"]
                                                                        .toString() ==
                                                                    "1"
                                                                ? Padding(
                                                                    padding:
                                                                        new EdgeInsets.fromLTRB(
                                                                            5,
                                                                            0,
                                                                            0,
                                                                            0),
                                                                    child: Text(
                                                                        '已保修',
                                                                        style:
                                                                            TextStyle(
                                                                          fontSize:
                                                                              11,
                                                                          color: Color.fromRGBO(
                                                                              18,
                                                                              150,
                                                                              219,
                                                                              1),
                                                                        )))
                                                                : Container(
                                                                    height: 0),
                                                          ],
                                                        ),
                                                      )
                                                    : new Container(height: 0),
                                                car[index]['yishou']
                                                            .toString() ==
                                                        "1"
                                                    ? new Padding(
                                                        padding: new EdgeInsets
                                                                .fromLTRB(
                                                            10, 5, 0, 0),
                                                        child: Align(
                                                            alignment: Alignment
                                                                .bottomLeft,
                                                            child: new Text(
                                                              '此车已售',
                                                              style: TextStyle(
                                                                  fontSize: 16,
                                                                  fontWeight:
                                                                      FontWeight
                                                                          .bold),
                                                            )))
                                                    : new Container(height: 0),
                                                //is_online
                                                // new Padding(
                                                //     padding:
                                                //         new EdgeInsets.fromLTRB(
                                                //             10, 5, 0, 0),

                                                //     child: Align(
                                                //         alignment: Alignment
                                                //             .bottomLeft,
                                                //         child: Container(
                                                //           padding: EdgeInsets
                                                //               .symmetric(
                                                //                   vertical: 5,
                                                //                   horizontal:
                                                //                       6),
                                                //           child: Text(
                                                //             car[index]['status_string'] !=
                                                //                     null
                                                //                 ? car[index][
                                                //                     'status_string']
                                                //                 : '暂无状态',
                                                //             style: new TextStyle(
                                                //                 fontSize: 11,
                                                //                 color: Colors
                                                //                     .white),
                                                //           ),
                                                //           decoration:
                                                //               new BoxDecoration(
                                                //             color:
                                                //                 Color.fromRGBO(
                                                //                     200,
                                                //                     200,
                                                //                     200,
                                                //                     1),
                                                //             borderRadius:
                                                //                 new BorderRadius
                                                //                     .all(new Radius
                                                //                         .circular(
                                                //                     5.0)),
                                                //           ),
                                                //         ))),
                                                new Padding(
                                                  padding:
                                                      new EdgeInsets.fromLTRB(
                                                          10, 5, 0, 10),
                                                  child: new Row(
                                                    children: <Widget>[
                                                      Expanded(
                                                        flex: 1,
                                                        child: Text(
                                                          '¥' +
                                                              car[index][
                                                                      "car_price"]
                                                                  .toString() +
                                                              "万",
                                                          style: TextStyle(
                                                              color: Color
                                                                  .fromRGBO(
                                                                      255,
                                                                      99,
                                                                      71,
                                                                      1),
                                                              fontSize: 15),
                                                        ),
                                                      ),
                                                      //is_online
                                                      new Padding(
                                                          padding:
                                                              new EdgeInsets
                                                                      .fromLTRB(
                                                                  10, 5, 0, 0),
                                                          child: Align(
                                                              alignment: Alignment
                                                                  .bottomLeft,
                                                              child: Container(
                                                                padding: EdgeInsets
                                                                    .symmetric(
                                                                        vertical:
                                                                            5,
                                                                        horizontal:
                                                                            6),
                                                                child: Text(
                                                                  car[index]['status_string'] !=
                                                                          null
                                                                      ? car[index]
                                                                          [
                                                                          'status_string']
                                                                      : '暂无状态',
                                                                  style: new TextStyle(
                                                                      fontSize:
                                                                          11,
                                                                      color: Colors
                                                                          .white),
                                                                ),
                                                                decoration:
                                                                    new BoxDecoration(
                                                                  color: Color
                                                                      .fromRGBO(
                                                                          200,
                                                                          200,
                                                                          200,
                                                                          1),
                                                                  borderRadius:
                                                                      new BorderRadius
                                                                          .all(new Radius
                                                                              .circular(
                                                                          5.0)),
                                                                ),
                                                              ))),
                                                      Expanded(
                                                          flex: 1,
                                                          child: Container(
                                                            height: 25,
                                                            child: FlatButton(
                                                              onPressed: () {
                                                                // Navigator.push(
                                                                //   context,
                                                                //   new MaterialPageRoute(
                                                                //       builder: (context) =>
                                                                //           new BusinessEditcarPage(
                                                                //               car[index]
                                                                //                   [
                                                                //                   'id'])),
                                                                // ).then((value) =>
                                                                //     {loaddata()});
                                                                Navigator.of(
                                                                        context)
                                                                    .push(new MaterialPageRoute(
                                                                        builder:
                                                                            (_) {
                                                                  return new BusinessEditcarPage(
                                                                      car[index]
                                                                          [
                                                                          'id']);
                                                                })).then(
                                                                        (param) {
                                                                  // page = 1;
                                                                  // loaddata();
                                                                });
                                                                // print(index.toString());
                                                              },
                                                              child: Text(
                                                                '编辑',
                                                                style: TextStyle(
                                                                    color: Colors
                                                                        .white,
                                                                    fontSize:
                                                                        13),
                                                              ),
                                                            ),
                                                            decoration:
                                                                BoxDecoration(
                                                              image:
                                                                  DecorationImage(
                                                                image: AssetImage(
                                                                    "resource/images/zhanshi/round.png"),
                                                                fit: BoxFit
                                                                    .fitHeight,
                                                              ),
                                                            ),
                                                          ))
                                                    ],
                                                  ),
                                                ),
                                                new Padding(
                                                  padding:
                                                      new EdgeInsets.fromLTRB(
                                                          10, 5, 0, 10),
                                                  child: new Row(
                                                    children: <Widget>[
                                                      Expanded(
                                                          flex: 1,
                                                          child: Container(
                                                            height: 25,
                                                            child: FlatButton(
                                                              onPressed: () {
                                                                submitauthorder(
                                                                    car[index][
                                                                            'id']
                                                                        .toString());
                                                              },
                                                              child: Text(
                                                                '认证',
                                                                style: TextStyle(
                                                                    color: Colors
                                                                        .white,
                                                                    fontSize:
                                                                        13),
                                                              ),
                                                            ),
                                                            decoration: BoxDecoration(
                                                                borderRadius: BorderRadius
                                                                    .all(Radius
                                                                        .circular(
                                                                            12.0)),
                                                                color: Colours
                                                                    .bj_green_g1
                                                                // image:
                                                                //     DecorationImage(
                                                                //   image: AssetImage(
                                                                //       "resource/images/zhanshi/round.png"),
                                                                //   fit: BoxFit
                                                                //       .fitHeight,
                                                                // ),
                                                                ),
                                                          )),
                                                      Container(
                                                        width: 10,
                                                      ),
                                                      Expanded(
                                                          //  flex: ,
                                                          child: Container(
                                                        height: 25,
                                                        child: FlatButton(
                                                          onPressed: () {
                                                            if (car[index][
                                                                        "is_guratee"]
                                                                    .toString() ==
                                                                "1") {
                                                              Toast.show(
                                                                  "车辆正在保修期限内",
                                                                  context);
                                                              return;
                                                            }
                                                            checkcanbaoxiu(car[
                                                                    index]['id']
                                                                .toString());
                                                          },
                                                          child: Text(
                                                            '保修',
                                                            style: TextStyle(
                                                                color: Colors
                                                                    .white,
                                                                fontSize: 13),
                                                          ),
                                                        ),
                                                        decoration:
                                                            BoxDecoration(
                                                          color: Colours
                                                              .bj_green_g1,
                                                          borderRadius:
                                                              BorderRadius.all(
                                                                  Radius
                                                                      .circular(
                                                                          12.0)),
                                                          // image:
                                                          //     DecorationImage(
                                                          //   image: AssetImage(
                                                          //       "resource/images/zhanshi/round.png"),
                                                          //   fit: BoxFit
                                                          //       .fitHeight,
                                                          // ),
                                                        ),
                                                      )),
                                                      Container(
                                                        width: 10,
                                                      ),
                                                      Expanded(
                                                          flex: 1,
                                                          child: int.parse(car[
                                                                          index]
                                                                      [
                                                                      'status']) >
                                                                  1
                                                              ? Container(
                                                                  height: 0,
                                                                )
                                                              : Container(
                                                                  height: 25,
                                                                  child:
                                                                      FlatButton(
                                                                    onPressed:
                                                                        () {
                                                                      precheckgroup(car[index]
                                                                              [
                                                                              'id']
                                                                          .toString());
                                                                    },
                                                                    child: Text(
                                                                      '自检',
                                                                      style: TextStyle(
                                                                          color: Colors
                                                                              .white,
                                                                          fontSize:
                                                                              13),
                                                                    ),
                                                                  ),
                                                                  decoration: BoxDecoration(
                                                                      borderRadius:
                                                                          BorderRadius.all(Radius.circular(
                                                                              12.0)),
                                                                      color: Colours
                                                                          .bj_green_g1
                                                                      // image:
                                                                      //     DecorationImage(
                                                                      //   image: AssetImage(
                                                                      //       "resource/images/zhanshi/round.png"),
                                                                      //   fit: BoxFit
                                                                      //       .fitHeight,
                                                                      // ),
                                                                      ),
                                                                ))
                                                    ],
                                                  ),
                                                )
                                              ],
                                            ),
                                          )
                                        ],
                                      ))),
                            );
                          }),
                ),
                Positioned(
                  bottom: 0,
                  left: 10,
                  height: 50,
                  width: MediaQuery.of(context).size.width - 20,
                  child: Container(
                    // width: double.infinity,

                    // color: Color.fromRGBO(27, 182, 251, 1),
                    child: new Row(
                      children: <Widget>[
                        Expanded(
                            flex: 1,
                            child: Padding(
                                padding: new EdgeInsets.fromLTRB(0, 0, 10, 0),
                                child: new FlatButton(
                                  onPressed: () {
                                    int canshare = 0;
                                    for (int i = 0; i < car.length; i++) {
                                      if (car[i]['car_status'].toString() ==
                                          "2") {
                                        canshare++;
                                      }
                                    }
                                    if (canshare == 0 && checkAll == false) {
                                      Toast.show('没有符合条件的车源', context);
                                      return;
                                    }

                                    if (checkAll == false) {
                                      //未全选
                                      setState(() {
                                        checkAll = true;
                                      });
                                    } else {
                                      setState(() {
                                        checkAll = false;
                                      });
                                    }

                                    var temp = this.car;
                                    for (var i = 0; i < temp.length; i++) {
                                      if (checkAll == true &&
                                          temp[i]['car_status'].toString() ==
                                              "2") {
                                        temp[i]['select'] = "1";
                                      } else {
                                        temp[i]['select'] = "0";
                                      }
                                    }
                                    setState(() {
                                      car = temp;
                                    });
                                  },
                                  color: Colors.blue,
                                  child: Text(checkAll == true ? '取消全选' : '全选',
                                      style: TextStyle(color: Colors.white)),
                                  shape: RoundedRectangleBorder(
                                      side: BorderSide.none,
                                      borderRadius: BorderRadius.all(
                                          Radius.circular(20))),
                                ))),
                        Expanded(
                            flex: 1,
                            child: Padding(
                                padding: new EdgeInsets.fromLTRB(10, 0, 0, 0),
                                child: new FlatButton(
                                  onPressed: () {
                                    sharehandel();
                                  },
                                  color: Color.fromRGBO(225, 102, 82, 1),
                                  child: Text('分享',
                                      style: TextStyle(color: Colors.white)),
                                  shape: RoundedRectangleBorder(
                                      side: BorderSide.none,
                                      borderRadius: BorderRadius.all(
                                          Radius.circular(20))),
                                ))),
                        // new Checkbox(value:checkAll, onChanged: (check){
                        //               setState(() {
                        //                 checkAll=check;
                        //               });
                        //               var temp = this.car;
                        //               for(var i =0;i<temp.length;i++){
                        //                 if(check==true){
                        //                   temp[i]['select']="1";
                        //                 }else{
                        //                   temp[i]['select']="0";
                        //                 }
                        //               }
                        //               setState(() {
                        //                 car = temp;
                        //               });
                        //             }),
                        //             Text('全选',style: TextStyle(color: Colors.white)),
                      ],
                    ),

                    // child: new FlatButton(

                    //     onPressed: () {
                    //       // connect();
                    //       // subOrder(0);

                    //     },
                    //     child: new Text(
                    //       '全选',
                    //       style: TextStyle(color: Colors.black),
                    //     )),
                  ),
                )
              ])),
            ],
          ),
          // 下拉菜单
          GZXDropDownMenu(
            // controller用于控制menu的显示或隐藏
            controller: _dropdownMenuController,
            // 下拉菜单显示或隐藏动画时长
            animationMilliseconds: 100,
            // 下拉菜单，高度自定义，你想显示什么就显示什么，完全由你决定，你只需要在选择后调用_dropdownMenuController.hide();即可
            menus: [
              GZXDropdownMenuBuilder(
                  dropDownHeight: 40 * 8.0,
                  dropDownWidget: _buildAddressWidget((selectValue) {
                    _dropDownHeaderItemStrings[0] = selectValue;
                    _dropdownMenuController.hide();
                    setState(() {});
                  })),
              GZXDropdownMenuBuilder(
                  dropDownHeight: 40 * 8.0,
                  dropDownWidget:
                      _buildConditionListWidget(_brandSortConditions, (value) {
                    _selectBrandSortCondition = value;
                    _dropDownHeaderItemStrings[1] =
                        _selectBrandSortCondition.name == '全部'
                            ? '品牌'
                            : _selectBrandSortCondition.name;
                    _dropdownMenuController.hide();
                    setState(() {});
                  })),
              GZXDropdownMenuBuilder(
                  dropDownHeight: 40.0 * _distanceSortConditions.length,
                  dropDownWidget:
                      _buildPriceListWidget(_distanceSortConditions, (value) {
                    _selectDistanceSortCondition = value;
                    _dropDownHeaderItemStrings[2] =
                        _selectDistanceSortCondition.name;
                    _dropdownMenuController.hide();
                    setState(() {});
                  })),
            ],
          ),
        ],
      ),
    );
  }

  int _selectTempFirstLevelIndex = 0;
  int _selectFirstLevelIndex = 0;
  int _selectSecondLevelIndex = -1;

  _buildAddressWidget(void itemOnTap(String selectValue)) {
//    List firstLevels = new List<int>.filled(15, 0);
    List firstLevels = new List<String>.generate(BrandList.length, (int index) {
      // if (index == 0) {
      //   return '全部';
      // }
      return BrandList[index]['title'];
    });

    List secondLevels = BrandList.length > 0
        ? new List<String>.generate(
            BrandList[_selectTempFirstLevelIndex]['child'].length, (int index) {
            // if (_selectTempFirstLevelIndex == 0) {
            //   return '';
            // }
            return BrandList[_selectTempFirstLevelIndex]['child'][index]
                ["name"];
          })
        : [];

    return Row(
      children: <Widget>[
        Container(
          width: 100,
          child: ListView(
            children: firstLevels.map((item) {
              int index = firstLevels.indexOf(item);
              return GestureDetector(
                behavior: HitTestBehavior.opaque,
                onTap: () {
                  //if(_selectTempFirstLevelIndex == 0)return;
                  _selectTempFirstLevelIndex = index;

                  // if (_selectTempFirstLevelIndex == 0) {
                  //   itemOnTap('全城');
                  //   return;
                  // }
                  setState(() {});
                },
                child: Container(
                    height: 40,
                    color: _selectTempFirstLevelIndex == index
                        ? Colors.grey[200]
                        : Colors.white,
                    alignment: Alignment.center,
                    child: _selectTempFirstLevelIndex == index
                        ? Text(
                            '$item',
                            style: TextStyle(
                              color: Colors.blue,
                            ),
                          )
                        : Text('$item')),
              );
            }).toList(),
          ),
        ),
        Expanded(
          child: Container(
            color: Colors.grey[200],
            child: ListView(
              children: secondLevels.map((item) {
                int index = secondLevels.indexOf(item);
                return GestureDetector(
                    behavior: HitTestBehavior.opaque,
                    onTap: () {
                      print('二级选项');
                      print(BrandList[_selectFirstLevelIndex]['child'][index]);
                      setState(() {
                        brand = BrandList[_selectFirstLevelIndex]['child']
                            [index]['id'];
                      });
                      _selectSecondLevelIndex = index;
                      _selectFirstLevelIndex = _selectTempFirstLevelIndex;
                      itemOnTap(item);
                      loaddata(); //加载数据
                      // if (_selectSecondLevelIndex == 0) {
                      //   itemOnTap(firstLevels[_selectFirstLevelIndex]);
                      // } else {
                      //   itemOnTap(item);
                      // }
                    },
                    child: Container(
                      height: 40,
                      alignment: Alignment.centerLeft,
                      child: Row(children: <Widget>[
                        SizedBox(
                          width: 20,
                        ),
                        _selectFirstLevelIndex == _selectTempFirstLevelIndex &&
                                _selectSecondLevelIndex == index
                            ? Text(
                                '$item',
                                style: TextStyle(
                                  color: Colors.blue,
                                ),
                              )
                            : Text('$item'),
                      ]),
                    ));
              }).toList(),
            ),
          ),
        )
      ],
    );
  }

  _buildConditionListWidget(
      items, void itemOnTap(SortCondition sortCondition)) {
    return ListView.separated(
      shrinkWrap: true,
      scrollDirection: Axis.vertical,
      itemCount: items.length,
      // item 的个数
      separatorBuilder: (BuildContext context, int index) =>
          Divider(height: 1.0),
      // 添加分割线
      itemBuilder: (BuildContext context, int index) {
        SortCondition goodsSortCondition = items[index];
        return GestureDetector(
          behavior: HitTestBehavior.opaque,
          onTap: () {
            print('点击车型' + goodsSortCondition.name);
            setState(() {
              cartype = goodsSortCondition.name;
            });
            for (var value in items) {
              value.isSelected = false;
            }
            goodsSortCondition.isSelected = true;

            itemOnTap(goodsSortCondition);
            loaddata();
          },
          child: Container(
//            color: Colors.blue,
            height: 40,
            child: Row(
              children: <Widget>[
                SizedBox(
                  width: 16,
                ),
                Expanded(
                  child: Text(
                    goodsSortCondition.name,
                    style: TextStyle(
                      color: goodsSortCondition.isSelected
                          ? Colors.blue
                          : Colors.black,
                    ),
                  ),
                ),
                goodsSortCondition.isSelected
                    ? Icon(
                        Icons.check,
                        color: Colors.blue,
                        size: 16,
                      )
                    : SizedBox(),
                SizedBox(
                  width: 16,
                ),
              ],
            ),
          ),
        );
      },
    );
  }

  _buildPriceListWidget(items, void itemOnTap(SortCondition sortCondition)) {
    return ListView.separated(
      shrinkWrap: true,
      scrollDirection: Axis.vertical,
      itemCount: items.length,
      // item 的个数
      separatorBuilder: (BuildContext context, int index) =>
          Divider(height: 1.0),
      // 添加分割线
      itemBuilder: (BuildContext context, int index) {
        SortCondition goodsSortCondition = items[index];
        return GestureDetector(
          behavior: HitTestBehavior.opaque,
          onTap: () {
            print('点击价格' + goodsSortCondition.name);
            setState(() {
              price = goodsSortCondition.tag;
            });
            for (var value in items) {
              value.isSelected = false;
            }
            goodsSortCondition.isSelected = true;

            itemOnTap(goodsSortCondition);
            loaddata();
          },
          child: Container(
//            color: Colors.blue,
            height: 40,
            child: Row(
              children: <Widget>[
                SizedBox(
                  width: 16,
                ),
                Expanded(
                  child: Text(
                    goodsSortCondition.name,
                    style: TextStyle(
                      color: goodsSortCondition.isSelected
                          ? Colors.blue
                          : Colors.black,
                    ),
                  ),
                ),
                goodsSortCondition.isSelected
                    ? Icon(
                        Icons.check,
                        color: Colors.blue,
                        size: 16,
                      )
                    : SizedBox(),
                SizedBox(
                  width: 16,
                ),
              ],
            ),
          ),
        );
      },
    );
  }
}
